package com.yztc.orangeentertainment.ui.main.findfragment.findmain;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.exception.DbException;
import com.yztc.orangeentertainment.R;
import com.yztc.orangeentertainment.bean.findbean.FindDbBean;

import java.util.List;

/**
 * Created by My on 2016/10/8.
 */
public class FindSearchActivity extends AppCompatActivity implements View.OnClickListener {
    public static EditText findsearchEdt;
    public static TextView findsearchChoice;
    public static ImageView findsearchDel;
    public LinearLayout findsearchFragment;

    private FragmentManager manager;
    public static DbUtils dbUtils;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.findsearch);
        dbUtils = DbUtils.create(this, "sousuo.db");
        initView();
        initData();
    }

    public void initView() {
        findsearchEdt = (EditText) findViewById(R.id.findsearch_edt);
        findsearchChoice = (TextView) findViewById(R.id.findsearch_choice);
        findsearchDel = (ImageView) findViewById(R.id.findsearch_del);
        findsearchFragment = (LinearLayout) findViewById(R.id.findsearch_fragment);
    }

    public void initData() {
        manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(R.id.findsearch_fragment, new FindHotSearchFragment());
        transaction.commit();

        findsearchChoice.setOnClickListener(this);
        findsearchDel.setOnClickListener(this);
        //搜索框的监听事件，数据变化搜索内容变化，并将数据存储到数据库中，且不能重复收藏
        findsearchEdt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                if (s.length() != 0) {

                    List<FindDbBean> list = null;
                    try {
                        list = FindSearchActivity.dbUtils.findAll(FindDbBean.class);
                    } catch (DbException e) {
                        e.printStackTrace();
                    }

                    if (list != null && list.size() != 0) {
                        int i = 0;
                        //取消重复收藏
                        for (FindDbBean db : list) {
                            if (!String.valueOf(s).equals(db.getTitle())) {
                                i++;
                                if (i == FindHotSearchFragment.list.size()) {
                                    FindDbBean dbBean = new FindDbBean(String.valueOf(s));
                                    try {
                                        FindSearchActivity.dbUtils.save(dbBean);
                                    } catch (DbException e) {
                                        e.printStackTrace();
                                    }
                                }
                            } else {
                                break;
                            }
                        }
                    } else {
                        FindDbBean dbBean = new FindDbBean(String.valueOf(s));
                        try {
                            FindSearchActivity.dbUtils.save(dbBean);
                        } catch (DbException e) {
                            e.printStackTrace();
                        }
                    }

                    findsearchChoice.setText("搜索");
                    findsearchDel.setVisibility(View.VISIBLE);
                    FragmentTransaction transaction = manager.beginTransaction();
                    transaction.replace(R.id.findsearch_fragment, new FindStarSearchFragment
                            (String.valueOf(s)));
                    transaction.commit();

                } else {
                    findsearchChoice.setText("取消");
                }

            }
        });
    }


    @Override
    public void onClick(View v) {
        FragmentTransaction transaction = manager.beginTransaction();
        String strChoice = (String) findsearchChoice.getText();
        String strEt = String.valueOf(findsearchEdt.getText());
        switch (v.getId()) {
            //搜索+取消
            case R.id.findsearch_choice:
                if ("取消".equals(strChoice)) {
                    finish();
                } else if ("搜索".equals(strChoice)) {
                    transaction.replace(R.id.findsearch_fragment, new FindStarSearchFragment
                            (strEt));
                }
                break;
            //点搜索框X号的监听事件
            case R.id.findsearch_del:
                findsearchEdt.setText(null);
                findsearchDel.setVisibility(View.GONE);
                findsearchChoice.setText("取消");
                transaction.replace(R.id.findsearch_fragment, new FindHotSearchFragment());
                break;

        }
        transaction.commit();
    }
}
